<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="账号" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择"
          clearable
          style="width: 120px"
        >
          <el-option
            v-for="item in allList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职位名称" prop="userId">
        <el-select
          v-model="queryParams.postId"
          placeholder="请选择"
          clearable
          style="width: 120px"
        >
          <el-option
            v-for="item in postIdList"
            :key="item.id"
            :label="item.postName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="postName">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入职位名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['areas:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['areas:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="roleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="120" />

      <el-table-column
        label="账号"
        prop="userName"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="姓名"
        prop="nickName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="用户手机"
        prop="userPhone"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="职位ID"
        prop="postId"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="职位名称"
        prop="postName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="日工资"
        prop="daySalary"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="日社保"
        prop="daySi"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['areas:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['areas:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="账号" prop="userId">
          <el-select
            v-model="form.userId"
            placeholder="请选择角色"
            @change="userChange"
          >
            <el-option
              v-for="item in allList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="userPhone">
          <el-input
            v-model="form.userPhone"
            onkeyup="this.value = this.value.replace(/[^\d]/g,'');"
            type="number"
            oninput="if(value.length>11)value=value.slice(0,11)"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="职位">
          <el-select
            v-model="form.postId"
            placeholder="请选择角色"
            @change="postChange"
          >
            <el-option
              v-for="item in postIdList"
              :key="item.id"
              :label="item.postName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日工资" prop="daySalary">
          <el-input
            type="number"
            v-model="form.daySalary"
            placeholder="请输入日工资"
          />
        </el-form-item>
        <el-form-item label="日社保" prop="daySi">
          <el-input
            type="number"
            v-model="form.daySi"
            placeholder="请输入日社保"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserList,
  updateUser,
  UserInfo,
  delUser,
  addUser,
  getUserId,
  getPostId,
  getPostList,
} from "@/api/zhao/staffuser.js";

export default {
  name: "Role",
  dicts: ["sys_normal_disable"],
  data() {
    var checkPhone = (rule, value, callback) => {
      // 手机号验证
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3456789]\d{9}$/;
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postId: undefined,
        postName: undefined,
        userId: undefined,
        userName: undefined,
      },
      allList: [], //账号列表
      postIdList: [], //账号列表
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "账号ID不能为空", trigger: "blur" },
        ],
        daySalary: [
          { required: true, message: "日工资不能为空", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        daySi: [{ required: true, message: "日社保不能为空", trigger: "blur" }],
        // userPhone: [
        //   { required: true, message: "电话不能为空", trigger: "blur" },
        //   {validator: checkPhone, trigger: 'blur'}
        // ],
      },
    };
  },
  created() {
    this.getList();
    this.getlists();
  },
  methods: {
    postChange(e) {
      this.form.postName =
        this.postIdList[
          this.postIdList.findIndex((item) => item.id === e)
        ].postName;
    },
    userChange(e) {
      console.log(e, this.allList);
      this.form.userName =
        this.allList[
          this.allList.findIndex((item) => item.userId === e)
        ].userName;
      this.form.nickName =
        this.allList[
          this.allList.findIndex((item) => item.userId === e)
        ].nickName;

      this.form.userPhone =
        this.allList[
          this.allList.findIndex((item) => item.userId === e)
        ].phonenumber;
    },
    reset() {
      this.allList = [];
      this.form = {
        userId: "",
        userName: "",
        nickName: "",
        userPhone: "",
        postId: "",
        postName: "",
        daySalary: "",
        daySi: "",
      };
      this.resetForm("form");
    },
    getList() {
      this.loading = true;
      getUserList(this.queryParams).then((res) => {
        this.roleList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams.areaName = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getlists() {
      getPostList().then((res) => {
        this.allList = res.data;
      });
      getPostId().then((res) => {
        this.postIdList = res.data;
      });
    },
    handleUpdate(row) {
      this.title = "修改员工信息";
      this.getlists();
      const postId = row.id || this.ids;
      UserInfo({ id: postId }).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let postIds = "";
      if (row.id) {
        postIds = [row.id];
      } else {
        postIds = this.ids;
      }
      this.$modal
        .confirm('是否确认删除ID编号为"' + postIds + '"的数据项？')
        .then(function () {
          delUser({ ids: postIds }).then((res) => {
            console.log(res);
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getlists();
      this.open = true;
      this.title = "添加员工信息";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUser(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("123123", this.form);
            addUser(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
